package com.xinruke.hostels.systemmanage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.BusiConstant;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwFangtaiyanse;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.YwFangtaiyanseMapper;
import com.xinruke.hostels.systemmanage.service.RoomStatusColorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class RoomStatusColorServiceImpl implements RoomStatusColorService {

    @Autowired
    private YwFangtaiyanseMapper ywFangtaiyanseMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Override
    public ResultVO addDefault(RoomStatusColorAddDefaultDTO params) {
        Integer jiudianId = params.getJiudianId();
        int count = ywFangtaiyanseMapper.countByJiudianId(jiudianId);
        if (count > 0) {
            return ResultVO.fail("已经有默认数据");
        }
        int insertCount = ywFangtaiyanseMapper.insertDefault(jiudianId);
        if (insertCount <= 0) {
            return ResultVO.fail("新增默认数据失败");
        }
        return ResultVO.success();
    }

    @Override
    public ResultVO modify(RoomStatusColorModifyDTO params) {
        ResultVO resultVO = ResultVO.fail();
        if (params.getId() != null && params.getId() > 0) {
            YwFangtaiyanse detailFromDB = ywFangtaiyanseMapper.selectByPrimaryKey(params.getId());
            if (detailFromDB == null) {
                resultVO.setMessage("查询无记录");
                return resultVO;
            }
            if(!detailFromDB.getFangtaiyanse().equals(params.getFangtaiyanse())){
                YwFangtaiyanse queryVo = new YwFangtaiyanse();
                queryVo.setFangtaiyanse(params.getFangtaiyanse());
                queryVo.setJiudianId(detailFromDB.getJiudianId());
                queryVo.setShanchubiaoshi(BusiConstant.DELFLAG_0);
                int count = ywFangtaiyanseMapper.selectCount(queryVo);
                if (count > 0 ) {
                    resultVO.setMessage("已存在相同房态颜色记录，请修改");
                    return resultVO;
                }
            }
            YwFangtaiyanse modify = new YwFangtaiyanse();
            modify.setId(params.getId());
            modify.setFangtaiyanse(params.getFangtaiyanse());
            modify.setGengxinshijian(new Date());
            int count = ywFangtaiyanseMapper.updateByPrimaryKeySelective(modify);
            if (count > 0) {
                return ResultVO.success();
            } else {
                resultVO.setMessage("房态颜色更新失败");
            }
        }
        return resultVO;
    }

    @Override
    public ResultVO recover(RoomStatusColorRecovercDTO params) {
        ResultVO resultVO = ResultVO.fail();
        if (params.getId() != null && params.getId() > 0) {
            YwFangtaiyanse detailFromDB = ywFangtaiyanseMapper.selectByPrimaryKey(params.getId());
            if (detailFromDB == null) {
                resultVO.setMessage("查询无记录");
                return resultVO;
            }
            YwFangtaiyanse modify = new YwFangtaiyanse();
            modify.setId(detailFromDB.getId());
            modify.setFangtaiyanse(detailFromDB.getMorenyanse());
            modify.setGengxinshijian(new Date());
            int count = ywFangtaiyanseMapper.updateByPrimaryKeySelective(modify);
            if (count > 0) {
                return ResultVO.success();
            } else {
                resultVO.setMessage("房态颜色更新失败");
            }
        }
        return resultVO;
    }

    @Override
    public ResultVO<RowsDataVO<RoomStatusColorQueryResultDTO>> list(RoomStatusColorQueryListDTO params) {
        RowsDataVO<RoomStatusColorQueryResultDTO> dataVO = new RowsDataVO();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());

        MyExample example = new MyExample(YwFangtaiyanse.class, conditions, params.getOrderFields());
        Page<YwFangtaiyanse> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwFangtaiyanse> list = ywFangtaiyanseMapper.selectByExample(example);

        List<RoomStatusColorQueryResultDTO> dtoList = list.stream().map(n -> {
            RoomStatusColorQueryResultDTO dto = new RoomStatusColorQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    @Override
    public ResultVO<RoomStatusColorQueryResultDTO> detail(RoomStatusColorDetailDTO params) {
        RoomStatusColorQueryResultDTO dto = new RoomStatusColorQueryResultDTO();
        YwFangtaiyanse detail = ywFangtaiyanseMapper.selectByPrimaryKey(params.getId());
        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        return ResultVO.success("获取详情成功", dto);
    }

}
